package com.qiqi.web.man;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qiqi.dao.OrderMapper;
import com.qiqi.dao.PhoneNumberMapper;
import com.qiqi.domain.Order;
import com.qiqi.domain.OrderExample;
import com.qiqi.domain.OrderExample.Criteria;
import com.qiqi.domain.PhoneNumber;
import com.qiqi.service.order.OrderService;

@Controller
public class OrderController {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderService orderService;
	@Autowired
	private PhoneNumberMapper phoneNumberMapper;
	@RequestMapping("/man/order")
	public String tolist(){
		return "/man/order/list";
	}
	/**
	 * 查询订单信息
	 * @return
	 */
	@RequestMapping("/man/order/find")
	@ResponseBody
	public Map<String, Object> find(String num,String address,String uid,String username,int page ,int pagesize){
		OrderExample example = new OrderExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotEmpty(num)){
			criteria.andNumLike("%"+num+"%");
		}
		if(StringUtils.isNotEmpty(address)){
			criteria.andNumLike("%"+address+"%");
		}
		if(StringUtils.isNotEmpty(uid)){
			criteria.andNumLike("%"+uid+"%");
		}
		if(StringUtils.isNotEmpty(username)){
			criteria.andNumLike("%"+username+"%");
		}
		Map<String, Object> res = orderService.findOrderByPage(example, page, pagesize);
		return res;
	}
	/**
	 * 审核订单信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/man/order/check/{id}")
	@ResponseBody
	public Map<String, Object> check(@PathVariable Integer id){
		Map<String, Object> res=new HashMap<String, Object>();
		Order order = new Order();
		order.setId(id);
		order.setState(1);
		int state = orderMapper.updateByPrimaryKeySelective(order);
		PhoneNumber num = new PhoneNumber();
		order = orderMapper.selectByPrimaryKey(id);
		num.setId(order.getNumid());
		num.setSelled(1);
		//修改号码
		phoneNumberMapper.updateByPrimaryKeySelective(num);
		res.put("success", state>0);
		return res;
	}
}
